<template>
  <div class="course-edit-container">
    <!-- 返回箭头 -->
    <div class="header">
      <el-button 
        icon="el-icon-arrow-left"
        @click="handleBack"
        class="back-button"
      ></el-button>
    </div>
    
    <!-- 表单区域 -->
    <el-form 
      :model="form"
      ref="formRef"
      label-width="120px"
      class="course-form"
      :rules="rules"
    >
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="form.courseName" placeholder="请输入课程名称" />
      </el-form-item>
      
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="form.courseType" placeholder="请选择课程类型">
          <el-option label="线上课程" value="线上课程" />
          <el-option label="线下课程" value="线下课程" />
          <el-option label="混合课程" value="混合课程" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="开课时间" prop="startTime">
        <el-date-picker
          v-model="form.startTime"
          type="date"
          placeholder="选择开课时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="form.endTime"
          type="date"
          placeholder="选择结束时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option label="未开课" value="未开课" />
          <el-option label="已开课" value="已开课" />
          <el-option label="已结束" value="已结束" />
        </el-select>
      </el-form-item>
    </el-form>
    
    <!-- 底部按钮 -->
    <div class="button-group">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const formRef = ref(null)

// 表单验证规则
const rules = {
  courseName: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
  courseType: [{ required: true, message: '请选择课程类型', trigger: 'change' }],
  startTime: [{ required: true, message: '请选择开课时间', trigger: 'change' }],
  endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 表单数据
const form = reactive({
  id: '',
  courseName: '',
  courseType: '',
  startTime: '',
  endTime: '',
  status: ''
})

// 获取课程数据
const getCourseData = () => {
  // 在实际项目中，这里应该通过API获取数据
  const courseId = route.params.id
  // 模拟数据
  const mockCourses = Array.from({ length: 50 }, (_, i) => ({
    id: i + 1,
    courseName: `课程${i + 1}`,
    courseType: ['线上课程', '线下课程', '混合课程'][Math.floor(Math.random() * 3)],
    startTime: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
    endTime: new Date(Date.now() + Math.random() * 60 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
    status: ['未开课', '已开课', '已结束'][Math.floor(Math.random() * 3)]
  }))
  
  // 查找匹配的课程
  const course = mockCourses.find(item => item.id === Number(courseId))
  if (course) {
    Object.assign(form, course)
  } else {
    ElMessage.error('未找到课程数据')
    router.push('/classManage/index')
  }
}

// 返回上一页
const handleBack = () => {
  router.go(-1)
}

// 取消按钮
const handleCancel = () => {
  router.go(-1)
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 在实际项目中，这里应该提交表单数据到API
      ElMessage.success('课程更新成功')
      router.push('/classManage/index')
    }
  })
}

// 页面加载时获取数据
onMounted(() => {
  getCourseData()
})
</script>

<style scoped>
.course-edit-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 30px;
}

.back-button {
  font-size: 18px;
}

.course-form {
  margin-bottom: 40px;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>